package com.linkallcloud.web.session;

import java.util.HashMap;
import java.util.Map;

import com.linkallcloud.core.dto.Sid;
import com.linkallcloud.core.lang.Strings;
import com.linkallcloud.core.util.Utils;
import com.linkallcloud.core.www.ISimpleUser;

public class SimpleUser implements ISimpleUser {
    private static final long serialVersionUID = 5616598211106979901L;

    private String loginName;

    private String userType;// 用户类型
    private Sid app;// 当前登录的app
    private String[] menuPermissions;

    private String orgDataSource;// 数据源标识，用于多数据库情况

    private Map<String, String> esxtras;

    private int ttl;// unit:Minute
    private String nonce;
    private String salt;

    public SimpleUser() {
        super();
        this.ttl = 10;
        this.salt = Utils.getRandomID(16);
    }

    @Override
    public String getLoginName() {
        return loginName;
    }

    @Override
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    @Override
    public int getUserClazz() {
        return "Yw".equals(this.getUserType()) ? 0 : 1;
    }

    @Override
    public String getUserType() {
        return userType;
    }

    @Override
    public void setUserType(String userType) {
        this.userType = userType;
    }

    @Override
    public Sid getApp() {
        return app;
    }

    @Override
    public Long appId() {
        return app == null ? null : app.getId();
    }

    @Override
    public String appUuid() {
        return app == null ? null : app.getUuid();
    }

    @Override
    public String appCode() {
        return app == null ? null : app.getCode();
    }

    @Override
    public String appName() {
        return app == null ? null : app.getName();
    }

    public void setApp(Sid app) {
        this.app = app;
    }

    public void setAppInfo(Long appId, String appUuid, String appCode, String appName) {
        this.app = new Sid(appId, appUuid, appCode, appName);
    }

    @Override
    public String[] getMenuPermissions() {
        return menuPermissions;
    }

    public void setMenuPermissions(String[] menuPermissions) {
        this.menuPermissions = menuPermissions;
    }

    @Override
    public boolean hasMenuPermission(String res) {
        if (Strings.isBlank(res) || this.getMenuPermissions() == null || this.getMenuPermissions().length <= 0) {
            return false;
        }
        for (String resCode : getMenuPermissions()) {
            if (res.equals(resCode)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Map<String, String> getExtras() {
        return this.esxtras;
    }

    @Override
    public void setExtras(Map<String, String> esxtras) {
        this.esxtras = esxtras;
    }

    public void addExtra(String key, String value) {
        if (this.esxtras == null) {
            this.esxtras = new HashMap<String, String>();
        }
        this.esxtras.put(key, value);
    }

    public String getExtra(String key) {
        if (this.esxtras.containsKey(key)) {
            return this.esxtras.get(key);
        }
        return null;
    }

    @Override
    public String getOrgDataSource() {
        return orgDataSource;
    }

    public void setOrgDataSource(String orgDataSource) {
        this.orgDataSource = orgDataSource;
    }

    @Override
    public int getTtl() {
        return ttl;
    }

    @Override
    public void setTtl(int ttl) {
        this.ttl = ttl;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

}
